﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TFDP.Common
{
    public sealed class ThreadsafeQueue<T> : IDisposable
    {
        private Queue<T> queue;
        private object disposeLock;

        #region Constructor
        public ThreadsafeQueue()
        {
            this.disposeLock = new object();
            lock (this.disposeLock)
            {
                this.queue = new Queue<T>();
            }
        } 
        #endregion

        #region Queue Methods
        public void Enqueue(T item)
        {
            lock (disposeLock)
            {
                if (isDisposed)
                    throw new ObjectDisposedException("ThreadsafeQueue");

                this.queue.Enqueue(item);
            }
        }

        public T Dequeue()
        {
            lock (disposeLock)
            {
                if (isDisposed)
                    throw new ObjectDisposedException("ThreadsafeQueue");

                return this.queue.Dequeue();
            }
        }

        public T Peek()
        {
            lock (disposeLock)
            {
                if (isDisposed)
                    throw new ObjectDisposedException("ThreadsafeQueue");

                return this.queue.Peek();
            }
        }

        public void Clear()
        {
            lock (disposeLock)
            {
                if (isDisposed)
                    throw new ObjectDisposedException("ThreadsafeQueue");

                this.queue.Clear();
            }
        }
        #endregion

        #region Properties
        public int Count
        {
            get
            {
                lock (disposeLock)
                {
                    if (isDisposed)
                        throw new ObjectDisposedException("ThreadsafeQueue");

                    return this.queue.Count;
                }
            }
        }
        #endregion

        #region ToList
        public List<T> ToList()
        {
            lock (disposeLock)
            {
                if (isDisposed)
                    throw new ObjectDisposedException("ThreadsafeQueue");

                return this.queue.ToList();
            }
        }
        #endregion

        #region IDisposable Members
        private bool isDisposed;

        public bool IsDisposed
        {
            get
            {
                lock (disposeLock)
                {
                    return isDisposed;
                }
            }
        }

        public void Dispose()
        {
            Dispose(true);
        }

        private void Dispose(bool disposing)
        {
            lock (disposeLock)
            {
                if (isDisposed)
                    return;

                isDisposed = true;

                if (disposing)
                {
                    if (queue != null)
                    {
                        foreach (T item in queue)
                        {
                            if(item is IDisposable)
                                ((IDisposable)item).Dispose();
                        }
                    }
                }
            }

            GC.SuppressFinalize(this);
        }
        #endregion
    }
}
